package com.sh.service.impl;

import com.sh.dao.ParentDao;
import com.sh.dao.RoleDao;
import com.sh.dao.StaffDao;
import com.sh.dao.StudentDao;
import com.sh.dto.LoginObject;
import com.sh.service.LoginService;
import com.sh.service.RoleService;
import com.sh.util.Menu;
import com.sh.util.MenuUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sh.dto.Response;
import sh.dto.ResultDto;
import sh.entity.*;
import sh.util.JwtUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class LoginControllerImpl implements LoginService {

    @Resource
    private ParentDao parentDao;

    @Resource
    private StaffDao staffDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private RoleDao roleDao;
    @Override
    public ResultDto login(LoginObject loginObject) {
        String userName=loginObject.getUserName();
        String password=loginObject.getPassword();
        String role=loginObject.getRole();
        if (StringUtils.isEmpty(userName)||StringUtils.isEmpty(password)||StringUtils.isEmpty(role)) {
            return Response.error(1002,"登录信息不完整");
        }
        HashMap<String,Object> data=new HashMap<>();
        //定义一个字符串token
        String token="";
        Role dbRole=null;
        int userId=0;
        if (role.equals("家长")) {
            Parent dbParent = parentDao.findByParentName(userName);
            if (dbParent==null) {
                return Response.error(1001,"用户不存在");
            }
            if (!dbParent.getParentPassword().equals(password)) {
                return Response.error(1003,"密码不正确");
            }
            data.put("parent",dbParent);
            userId=dbParent.getParentId();
             dbRole = roleDao.getByName("家长");
        }
      else   if (role.equals("学生")) {
            Student dbStudent = studentDao.findByStudentCode(userName);
            if (dbStudent==null) {
                return Response.error(1001,"用户不存在");
            }
            if (!dbStudent.getStudentPassword().equals(password)) {
                return Response.error(1003,"密码不正确");
            }
            data.put("student",dbStudent);
            userId=dbStudent.getStudentId();
            dbRole = roleDao.getByName("学生");
        }
        else if (role.equals("班主任")) {
            Staff dbStaff = staffDao.findByNumber(userName);
            if (dbStaff==null) {
                return Response.error(1001,"用户不存在");
            }
            if (!dbStaff.getStaffPassword().equals(password)) {
                return Response.error(1003,"密码不正确");
            }
            data.put("staff",dbStaff);
            userId=dbStaff.getStaffId();
            dbRole = roleDao.getByName("班主任");
        }
        else if (role.equals("管理员")) {
            Staff dbStaff = staffDao.findByNumber(userName);
            if (dbStaff==null) {
                return Response.error(1001,"用户不存在");
            }
            if (!dbStaff.getStaffPassword().equals(password)) {
                return Response.error(1003,"密码不正确");
            }
            dbRole = roleDao.getByName("管理员");
            data.put("staff",dbStaff);
            userId=dbStaff.getStaffId();
        }
        else  if (role.equals("任课老师")) {
            Staff dbStaff = staffDao.findByNumber(userName);
            if (dbStaff==null) {
                return Response.error(1001,"用户不存在");
            }
            if (!dbStaff.getStaffPassword().equals(password)) {
                return Response.error(1003,"密码不正确");
            }
            dbRole = roleDao.getByName("任课老师");
            data.put("staff",dbStaff);
            userId=dbStaff.getStaffId();
        }
        else {
            return Response.error(1005,"角色不对");
        }
        List<Auth> authList = dbRole.getAuthList();
        List<Menu> menus = MenuUtil.getMenus(authList, false);
        data.put("menus",menus);
        //data.put("authList",authList);
        //data.put("userId",userId);
         token = JwtUtil.sign(data, userId);

        return Response.success(200,"登录成功",token);
    }


}
